package com.nbjtjc.safe.api.emg;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgTypeMapper;
import com.nbjtjc.safe.model.emg.EmgType;
import com.nbjtjc.safe.service.emg.EmgTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应急类型
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgType")
public class EmgTypeController extends AbstractController<EmgType> {

    @Autowired
    private EmgTypeService emgTypeService;
    @Autowired
    private EmgTypeMapper emgTypeMapper;

    @Override
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    @SysLog("删除应急预案类型")
    public Result delete(int id) {
        Result result=new Result();
        try {
            emgTypeMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
    @SysLog("添加应急预案类型")
    public Result add(EmgType emgType) {
        Result result=new Result();
        if(ObjectUtil.isNull(emgType)){
            result.setFalse();
            return result;
        }
        try {
            if(emgType.getId()!=null&&emgType.getId()!=0){
                emgTypeMapper.updateByPrimaryKeySelective(emgType);
            }else{
                emgTypeMapper.insertSelective(emgType);
            }
            result.setData(emgType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/find",method = {RequestMethod.POST})
    public Result find(EmgType emgType, Page page) {
        Result result=new Result();
        try {
            List<EmgType> emgTypes=emgTypeService.find(emgType);
            result.setData(emgTypes);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @GetMapping(value = "/getAll/{companyId}")
    public Result getAll(@PathVariable int companyId) {
        Result result=new Result();
        try {
            List<EmgType> emgTypes=emgTypeService.getAll(companyId);
            result.setData(emgTypes);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    /**
     * 复制应急响应类型和应急预案和应急物资
     * @param fromCompanyId
     * @param toCompanyId
     * @return
     */
    @GetMapping("/copyEmgTypeAndPlan/{fromCompanyId}/{toCompanyId}")
    public Result copyEmgTypeAndPlan(@PathVariable int fromCompanyId,@PathVariable int toCompanyId) {
        Result result=new Result();
        emgTypeService.copyEmgType(fromCompanyId,toCompanyId);
        return result;
    }
    @RequestMapping(value = "/typePath",method = {RequestMethod.POST})
    public Result pathIds(int typeId){
        Result result=new Result();
        try {
            result=emgTypeService.getPath(typeId);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

}
